package com.rdc.qingkou.ui;


import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;

import java.io.IOException;



public class PreviewDialogActivity extends AppCompatActivity
       implements
        MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener,
        View.OnClickListener{
    private SurfaceView mPreview;
    private ProgressBar mProgress;
    private ProgressBar mSeek;
    private ImageView mPreviewPhoto;
    private SurfaceHolder mSurfaceHolder;
    private MediaPlayer mMediaPlayer;
    private Button mBack;
    private Button mDelete;
    private boolean mSeekBarAuto;
    private boolean mIsImage;
    private ImageView mStop;
    private Uri mUri;
    private int mPlayPosition;
    private Thread thread = new Thread(){
        @Override
        public void run() {
            try{
                while (mSeekBarAuto){
                    if (null!=mMediaPlayer&&mMediaPlayer.isPlaying()){
                        mSeek.setProgress(mMediaPlayer.getCurrentPosition());
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        setContentView(R.layout.send_preview);
        StatusBarCompat.setStatusBarColor(this,getResources().getColor(R.color.windowBackground),true);
        Intent intent = getIntent();
        intent.getStringExtra("MediaPath");
        mIsImage = intent.getStringExtra("MediaPath").endsWith("jpg")||intent.getStringExtra("MediaPath").endsWith("png")||
                intent.getStringExtra("MediaPath").endsWith("JPEG");
        mUri = Uri.parse(intent.getStringExtra("MediaPath"));
        init();
        initview();

    }



    private void init() {
        mPreview = (SurfaceView) findViewById(R.id.sv_preview);
        mProgress = (ProgressBar) findViewById(R.id.pb_progress);
        mSeek = (ProgressBar) findViewById(R.id.seekBar);
        mPreviewPhoto = (ImageView)findViewById(R.id.iv_preview);
        mStop = (ImageView)findViewById(R.id.iv_stop);
        mBack = (Button)findViewById(R.id.btn_back);
        mDelete = (Button)findViewById(R.id.btn_delete);
        mStop.setVisibility(View.GONE);
        mBack.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mPreview.setOnClickListener(this);
        mSeek.setProgress(0);
        mSeekBarAuto = false;
        mPlayPosition= -1;
        mSeekBarAuto = true;
        thread.start();

    }




    public void initview(){
        if (mIsImage){
            mPreview.setVisibility(View.GONE);
            mSeek.setVisibility(View.GONE);
            mStop.setVisibility(View.GONE);
            mProgress.setVisibility(View.GONE);
            mPreview.setEnabled(false);
            Glide.with(this).load(mUri.getPath()).into(mPreviewPhoto);
        }else {
            mSurfaceHolder = mPreview.getHolder();
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            mSurfaceHolder.addCallback(new SurfaceCallback());
        }

    }

    private class SurfaceCallback implements SurfaceHolder.Callback{
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            playVideo();
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            if (mMediaPlayer!=null){
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
        }
    }


    public void playVideo(){

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.reset();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);


        try{

            mMediaPlayer.setDataSource(this, mUri);
            mMediaPlayer.prepareAsync();
        }catch (IOException e){
            e.printStackTrace();
            Toast.makeText(this,"加载视频错误",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mProgress.setVisibility(View.GONE);
        mediaPlayer.setDisplay(mSurfaceHolder);
        mediaPlayer.setScreenOnWhilePlaying(true);
        mSurfaceHolder.setKeepScreenOn(true);
        mSeek.setProgress(0);
        mSeek.setMax(mediaPlayer.getDuration());
        mediaPlayer.start();
        mPlayPosition = mMediaPlayer.getCurrentPosition();
        mediaPlayer.pause();
        mStop.setVisibility(View.VISIBLE);




    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        if (null != mediaPlayer) {
            mediaPlayer.seekTo(0);
            mSeek.setProgress(0);
            mediaPlayer.pause();
            mPlayPosition = mMediaPlayer.getCurrentPosition();
            mStop.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        mSeekBarAuto = false;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.sv_preview:
                if (null!=mMediaPlayer){
                    if (mMediaPlayer.isPlaying()){
                        mPlayPosition = mMediaPlayer.getCurrentPosition();
                        Log.d("pause",""+mPlayPosition);
                        mMediaPlayer.pause();
                        mStop.setVisibility(View.VISIBLE);
                    }else {
                        if (mPlayPosition>-1){
                            mPlayPosition = mMediaPlayer.getCurrentPosition();
                            Log.d("start",""+mPlayPosition);
                            mMediaPlayer.start();
                            mStop.setVisibility(View.GONE);
                            mPlayPosition = -1;

                        }

                        }

                }
                break;
            case R.id.btn_back:{
                Intent intent = new Intent();
                intent.putExtra("MediaPath", mUri.getPath());
                setResult(RESULT_OK,intent);
                finish();
            }

            break;
            case  R.id.btn_delete:
                Intent intent = new Intent();
                intent.putExtra("MediaPath","");
                setResult(RESULT_OK,intent);
                finish();
                break;
            default:
                break;



        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        mSeekBarAuto = false;
        thread = null;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mSeekBarAuto = true;
        thread = new Thread(){
            @Override
            public void run() {
                try{
                    while (mSeekBarAuto){
                        if (null!=mMediaPlayer&&mMediaPlayer.isPlaying()){
                            mSeek.setProgress(mMediaPlayer.getCurrentPosition());
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
}
